package icbc.nav.control
{
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	
	import icbc.app.AppGlobal;
	
	import org.osflash.signals.Signal;
	
	public class ContentNavBarreControl extends Sprite
	{
		public var panel:Sprite;
		public var viewWidth:uint = 50;
		public var viewHeight:uint = 40;
		public var onClicked:Signal = new Signal(int);
		private var leftItem:ContentNavButtonControl;
		private var rightItem:ContentNavButtonControl;

		public function ContentNavBarreControl()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
	
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			initContent();
		}
		
		private function initContent():void
		{
			panel = setBasePanel();
			addChild(panel);
			
			var arrowWidth:uint = 13;
			var arrowHeight:uint = viewHeight;
			
			leftItem = new ContentNavButtonControl(arrowWidth,arrowHeight);
			panel.addChild(leftItem);
			leftItem.onClick.add(leftClickHandler);

			rightItem = new ContentNavButtonControl(arrowWidth,arrowHeight);
			panel.addChild(rightItem);
			rightItem.onClick.add(rightClickHandler);

			leftItem.x = arrowWidth/2;
			leftItem.y = viewHeight/2 - leftItem.height/2;
			leftItem.scaleX = -1;
			
			rightItem.x = leftItem.x + arrowWidth + 12;
			rightItem.y = viewHeight/2 - rightItem.height/2;
			initBlink();
		}
		
		private function leftClickHandler():void
		{
			onClicked.dispatch(0);
		}

		private function rightClickHandler():void
		{
			onClicked.dispatch(1);
		}
		
		protected function setBasePanel():Sprite
		{
			return buildPlainPanel();
		}
		
		protected function buildPlainPanel():Sprite
		{
			var base:Sprite = new Sprite();
			base.graphics.beginFill(0x000000,0);
			base.graphics.drawRect(0, 0, viewWidth, viewHeight);
			base.graphics.endFill();
			return base;
		}
		
		private function initBlink():void
		{
			var tween:TweenLite = new TweenLite(this, AppGlobal.BLINK_TIME_OUT, {onComplete:onBlinkHandler});
		}
		
		private function onBlinkHandler():void
		{
			leftItem.isBlinkStop = false;
			rightItem.isBlinkStop = false;
			leftItem.blink();
			rightItem.blink();
			leftItem.onBlinkStop.add(blinkStopHandler);
			rightItem.onBlinkStop.add(blinkStopHandler);
		}
		
		private function blinkStopHandler():void
		{
			leftItem.stopBlink();
			rightItem.stopBlink();
		}
	}
}
